package model

import (
	"context"
	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ PmsProductAttributeCategoryModel = (*customPmsProductAttributeCategoryModel)(nil)

type (
	// PmsProductAttributeCategoryModel is an interface to be customized, add more methods here,
	// and implement the added methods in customPmsProductAttributeCategoryModel.
	PmsProductAttributeCategoryModel interface {
		pmsProductAttributeCategoryModel
		Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
		RowBuilder() squirrel.SelectBuilder
		CountBuilder(field string) squirrel.SelectBuilder
		FindByPageParam(ctx context.Context, rowBuilder squirrel.SelectBuilder, pageNum, pageSize int64) ([]*PmsProductAttributeCategory, error)
		FindCount(ctx context.Context, countBuilder squirrel.SelectBuilder) (int64, error)
	}

	customPmsProductAttributeCategoryModel struct {
		*defaultPmsProductAttributeCategoryModel
	}
)

// NewPmsProductAttributeCategoryModel returns a model for the database table.
func NewPmsProductAttributeCategoryModel(conn sqlx.SqlConn, c cache.CacheConf) PmsProductAttributeCategoryModel {
	return &customPmsProductAttributeCategoryModel{
		defaultPmsProductAttributeCategoryModel: newPmsProductAttributeCategoryModel(conn, c),
	}
}

func (m *defaultPmsProductAttributeCategoryModel) FindByPageParam(ctx context.Context, rowBuilder squirrel.SelectBuilder, pageNum, pageSize int64) ([]*PmsProductAttributeCategory, error) {

	if pageSize < 1 {
		pageSize = 1
	}
	if pageNum < 1 {
		pageNum = 1
	}
	offset := (pageNum - 1) * pageSize
	query, values, err := rowBuilder.Limit(uint64(pageSize)).Offset(uint64(offset)).ToSql()
	if err != nil {
		return nil, err
	}
	var resp []*PmsProductAttributeCategory
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return nil, err
	}
}

func (m *defaultPmsProductAttributeCategoryModel) FindCount(ctx context.Context, countBuilder squirrel.SelectBuilder) (int64, error) {
	query, values, err := countBuilder.ToSql()
	if err != nil {
		return 0, err
	}
	var resp int64
	err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return 0, err
	}
}

// export logic
func (m *defaultPmsProductAttributeCategoryModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
	return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
		return fn(ctx, session)
	})

}

//RowBuilder export logic
func (m *defaultPmsProductAttributeCategoryModel) RowBuilder() squirrel.SelectBuilder {
	return squirrel.Select(pmsProductAttributeCategoryRows).From(m.table)
}

//CountBuilder export logic
func (m *defaultPmsProductAttributeCategoryModel) CountBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("COUNT(" + field + ")").From(m.table)
}

//SumBuilder export logic
func (m *defaultPmsProductAttributeCategoryModel) SumBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("IFNULL(SUM(" + field + "),0)").From(m.table)
}
